package com.xiaxiang.xiawan.Util;

import android.content.Context;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * Created by Administrator on 2017-10-29.
 */

public class XXToolUtil {
    public static String imageurl = "https://mobile.umeng.com/images/pic/home/social/img-1.png";
    //     public static String imageurl = "https://img.dawuhanmedia.com/liveimg/41/1490773976999103661055569.jpg";
    public static String videourl = "http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html";
    public final static String MATERIAL_ID_KEY = "materialId";
    public final static String MATERIAL_URL_KEY = "materialUrl";
    public final static String USER_NAME_KEY = "userName";
    public final static String NAME_KEY = "name";
    public final static String MATERIAL_NAME_KEY = "materialName";
    public final static String AR_MODE_KEY = "arMode";
    public final static String LOGINDATA_KEY = "loginData";
    public final static String MATERIAL_VIDEO_DATA_KEY = "arMaterialVideoData";
    public static void showTip(Context context,String text){
        Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
    }

    public static String getFileMD5(String filePath) {
        String md5=null;
        File file = new File(filePath);
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;//messagedigest一定要为局部变量，为了保证线程安全。写成全局变量在多线程情况下，md5值计算将出现问题。
        FileInputStream in = null;
        byte buffer[] = new byte[8192];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            BigInteger bigInt = new BigInteger(1, digest.digest());
            md5= bigInt.toString(16);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return md5;
    }
    private static char[] hexChar = {
            '0','1','2','3','4','5','6','7','8','9',
            'a','b','c','d','e','f'
    };

    protected static String toHexString(byte[] b){
        StringBuilder sb = new StringBuilder(b.length*2);
        for(int i=0;i<b.length;i++){
            sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
            sb.append(hexChar[b[i] & 0x0f]);
        }
        return sb.toString();
    }
}
